package com.hq.fxh.Utils;

/**
 * /**
 *
 * @author 薛
 * @ClassName
 * @Description :
 * @Created 2017/8/16
 * @Email xlwenhao1858@sina.com
 */


import com.hq.fxh.BuildConfig;
import com.hq.fxh.HttpApi.ArticleService;
import com.hq.fxh.HttpApi.ArticleServiceImpl;
import com.hq.fxh.HttpApi.UserServiceImpl;
import com.hq.fxh.No0_ChengQu.Activity.NullOnEmptyConverterFactory;
import com.hq.fxh.HttpApi.UserService;

import java.io.IOException;
import java.util.Date;
import java.util.concurrent.TimeUnit;


import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by bruce on 16/4/22.
 */
public class HttpHelper {
    //    public static final String END_POINT = "http://192.168.66.129:8888/";
//    public static final String END_POINT = "https://demoapi.ledianduo.com/";
    public static final String END_POINT = "https://mobileapi.ledianduo.com/";

    public static void initialize() {
        Interceptor interceptor = new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request()
                        .newBuilder()
//                version 版本号
//                device_type 类型 1 iOS 2Android
//                device_code 设备号
//                ts 当前时间戳
//                uid 当前登录用户id  没登录的0
//                token 登录/注册成功返回的token
                        .addHeader("version", "4.1.1")
                      /*  .addHeader("device_code", AppUtils.getDeviceInfo(AppUtils.sApp))*/

                        .addHeader("device_type", "2")
                        .addHeader("uid", 14227 + "")
                        .addHeader("token", "dayi4F9G90WPREWSpF9qeQ")
                        .addHeader("ts", String.valueOf(new Date().getTime()))
                        .build();

                if (BuildConfig.DEBUG) {
                    StringBuilder sb = new StringBuilder();
                    if (request.body() instanceof FormBody) {
                        FormBody body = (FormBody) request.body();
                        for (int i = 0; i < body.size(); i++) {
                            sb.append(body.encodedName(i)).append("=").append(body.encodedValue(i)).append(",");
                        }
                        if (sb.length() >= 1) {
                            sb.delete(sb.length() - 1, sb.length());
                        }

                    } else {

                    }
                }

                Response response = chain.proceed(request);
                final String responseString = new String(response.body().bytes());

                return response.newBuilder()
                        .body(ResponseBody.create(response.body().contentType(), responseString))
                        .build();
            }
        };

        OkHttpClient client = new OkHttpClient.Builder()
                .addNetworkInterceptor(interceptor)
                .retryOnConnectionFailure(true)
                .connectTimeout(100, TimeUnit.SECONDS)//15
                .build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(END_POINT)
                .client(client)
                .addConverterFactory(new NullOnEmptyConverterFactory())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();

        UserServiceImpl.userService = retrofit.create(UserService.class);
        ArticleServiceImpl.articleService = retrofit.create(ArticleService.class);

    }
}
